package com.asset.modules.common.controller;

import com.asset.modules.assets.service.AssetService;
import com.asset.modules.budget.service.BudgetService;
import com.asset.modules.common.model.ParamBean;
import com.asset.modules.common.model.ResponseBean;
import com.asset.modules.system.service.CompanyService;
import com.asset.modules.system.service.ProjectService;
import com.asset.modules.system.service.RoleService;
import com.asset.modules.system.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;


@RestController
@CrossOrigin
@RequestMapping("/common")
@Api(tags = "公共接口")
public class CommonController {
    private static final Logger logger = LoggerFactory.getLogger(CommonController.class);

    @Autowired
    AssetService assetService;

    @Autowired
    BudgetService budgetService;

    @Autowired
    CompanyService companyService;

    @Autowired
    ProjectService projectService;

    @Autowired
    RoleService roleService;

    @Autowired
    UserService userService;



    @ApiOperation(value = "检查字段值是否存在")
    @RequestMapping(value = "/isExist", method = RequestMethod.POST)
    @ResponseBody
    public ResponseBean isExist(@RequestBody ParamBean param){
        boolean flag;
        switch (param.getClassName()){
            case "asset":
                flag = assetService.isExist(param.getColumn(), param.getValue());
                break;
            case "budget":
                flag = budgetService.isExist(param.getColumn(), param.getValue());
                break;
            case "company":
                flag = companyService.isExist(param.getColumn(), param.getValue());
                break;
            case "project":
                flag = projectService.isExist(param.getColumn(), param.getValue());
                break;
            case "role":
                flag = roleService.isExist(param.getColumn(), param.getValue());
                break;
            case "user":
                flag = userService.isExist(param.getColumn(), param.getValue());
                break;
            default:
                flag = false;
                break;
        }

       return new ResponseBean(HttpStatus.OK.value(), "请求成功", flag);
    }

//    @Autowired
//    private GameInfoService gameService;
//    @Autowired
//    private OssService ossService;
//
//    @RequestMapping(value = "/getPicUrl", method = RequestMethod.GET)
//    public ResponseBean queryGamePicsByGameId(@RequestParam String bucketName, @RequestParam String filePath, @RequestParam String fileName){
//        String url = ossService.generateOssFileOssUrl(bucketName,filePath,fileName);
//        return new ResponseBean(HttpStatus.OK.value(), "游戏图片列表返回成功", url);
//    }
//
//    /**
//     * 上传到公共区域oss
//     *
//     * @param file
//     * @param ossRootPath
//     * */
//    @RequestMapping(value = "/uploadPic", method = RequestMethod.POST)
//    public ResponseBean uploadGamePic(@RequestParam(value = "file", required = false) MultipartFile file, @RequestParam(value = "ossRootPath", required = false) String ossRootPath) {
//        try {
//            String fileName = ossService.uploadPubPic(file,ossRootPath);
//            return new ResponseBean(HttpStatus.OK.value(), "游戏图片上传成功", fileName);
//        }catch (Exception e){
//            e.printStackTrace();
//            throw new CustomException("图片上传失败!");
//        }
//    }
//    /**
//     * 富文本上传多张图片接口
//     *
//     * @param files
//     * */
//    @RequestMapping(value = "/uploadBanthPic", method = RequestMethod.POST)
//    public ResponseBean uploadBanthPic(@RequestParam(value = "files", required = false) MultipartFile[] files) {
//        try {
//            List<String> fileNames = new ArrayList<>();
//            if(files != null && files.length > 0){
//                //循环获取file数组中得文件
//                for(int i = 0;i < files.length;i++){
//                    MultipartFile file = files[i];
//                    //保存文件
//                    if (!file.isEmpty()){
//                        String fileName = ossService.uploadPubPic(file,"richContent");
//                        fileNames.add("http://asset-pub.oss-cn-qingdao.aliyuncs.com/richContent/"+fileName);
//                    }
//                }
//            }
//            return new ResponseBean(HttpStatus.OK.value(), "图片上传成功", fileNames);
//        }catch (Exception e){
//            e.printStackTrace();
//            throw new CustomException("图片上传失败!");
//        }
//    }



}
